from rest_framework import serializers
from .models import *

#用户
class UserSer(serializers.ModelSerializer):
    class Meta:
        model=Users
        fields='__all__'


class AreaSer(serializers.ModelSerializer):
    class Meta:
        model=Area
        fields='__all__'

class CategorySer(serializers.ModelSerializer):
    class Meta:
        model=Category
        fields='__all__'

class CommentSer(serializers.ModelSerializer):
    class Meta:
        model=Comment
        fields='__all__'

class ShowCommentSer(serializers.ModelSerializer):
    user = UserSer()
    class Meta:
        model=Comment
        fields='__all__'

class GoodsSer(serializers.ModelSerializer):
    comment_set = ShowCommentSer(many=True,read_only=True)
    users = UserSer(many=True,read_only=True)
    class Meta:
        model=Goods
        fields='__all__'
        
class AdvertSer(serializers.ModelSerializer):
    class Meta:
        model=Advert
        fields='__all__'
        
class AddressSer(serializers.ModelSerializer):
    class Meta:
        model=Address
        fields='__all__'

class OrderDetailSer(serializers.ModelSerializer):
    class Meta:
        model=OrderDetail
        fields='__all__'

class OrdersSer(serializers.ModelSerializer):
    create_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S')
    details = OrderDetailSer(many=True,read_only=True)
    class Meta:
        model=OrderInfo
        fields='__all__'


class SFLoginSer(serializers.ModelSerializer):
    userid = UserSer()
    class Meta:
        model=SFLogin
        fields='__all__'

# class ESTESTSer(serializers.ModelSerializer):
#     class Meta:
#         model=ESTEST
#         fields='__all__'

# from .search_indexes import ESTESTIndex
# from drf_haystack.serializers import HaystackSerializer

# class ESTESTIndexSerializer(HaystackSerializer):
#     object = ESTESTSer(read_only=True)
#     """
#     SKU索引结果数据序列化器
#     """
#     class Meta:
#         index_classes = [ESTESTIndex]
#         fields = ('text', 'name', 'content','type','id','object')

from drf_haystack.serializers import HaystackSerializer
from rest_framework.serializers import ModelSerializer



from users.search_indexes import BookIndex

class BookSerializer(ModelSerializer):
    class Meta:
        model=Book
        fields='__all__'

class BookIndexSerializer(HaystackSerializer):
    object = BookSerializer(read_only=True) # 只读,不可以进行反序列化

    class Meta:
        index_classes = [BookIndex]# 索引类的名称
        fields = ('text','name','publish','object')# text 由索引类进行返回, object 由序列化类进行返回,第一个参数必须是text

